package com.hk.core.autoconfigure.data.jdbc;

import com.hk.core.jdbc.JdbcSession;
import com.hk.core.jdbc.inspect.SqlInspector;
import com.hk.core.jdbc.dialect.Dialect;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

/**
 * @author Kevin
 * @date 2019-9-11 20:55
 */
@AutoConfiguration(after = JdbcTemplateAutoConfiguration.class)
@ConditionalOnSingleCandidate(value = NamedParameterJdbcTemplate.class)
@ConditionalOnClass(value = {JdbcSession.class, NamedParameterJdbcTemplate.class})
public class JdbcAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(Dialect.class)
    public Dialect mysqlDialect() {
        return Dialect.mysql();
    }

    @Bean
    public JdbcSession jdbcSession(NamedParameterJdbcTemplate jdbcTemplate, Dialect dialect,
                                   ObjectProvider<SqlInspector> sqlInspectorProvider) {
        return new JdbcSession(jdbcTemplate, dialect, sqlInspectorProvider.getIfAvailable(SqlInspector::empty));
    }

}
